<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="财务确认" v-model="visible" align-center width="800px" :z-index="2000" @close="handleClose">
    <el-form ref="formRef" :model="form" class="h-44" label-width="110px">
      <el-form-item label="审核">
        <el-radio-group v-model="form.confirm_result" class="ml-4">
          <el-radio :value="1">通过</el-radio>
          <el-radio :value="2">驳回</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="form.confirm_result === 2" label="驳回原因">
        <el-input v-model="form.confirm_remark" type="textarea" :autosize="{ minRows: 5 }"></el-input>
      </el-form-item>
      <template v-else>
        <el-form-item label="收款账号" prop="refund_account" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.refund_account" placeholder="请选择" :options="options.payment_account" class="w-full" />
        </el-form-item>
        <el-form-item label="退款到账时间" prop="refund_received_time" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-date-picker v-model="form.refund_received_time" placeholder="日期选择" type="date" value-format="YYYY-MM-DD" class="w-full" />
        </el-form-item>
      </template>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
import { resMessage } from '@/utils/res'

const props = defineProps({
  options: { type: Object, default: () => ({}) },
})

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

const emit = defineEmits(['close', 'success'])

const form = ref({})
const formRef = ref()

// 打开弹窗
const open = async (r_ids) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = { r_ids, confirm_result: 1 }
  loadings = reactive({})
  visible.value = true
  formRef?.value?.resetFields()
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = async() => {
  if (!await formRef?.value.validate()) return
  loadings.submit = true
  api.financeConfirm(form.value).then(res => {
    resMessage({
      res,
      success: () => {
        ElMessage.success('操作成功')
        emit('success')
        close()
      }
    })
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>